package com.whc.gulimall.scekill.interceptor;

import com.whc.common.constant.SocialLoginConstant;
import com.whc.common.vo.SocialResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义订单模块拦截器
 *
 * @author angaoen
 */
@Component
public class SecKillInterceptor implements HandlerInterceptor {

    public static ThreadLocal<SocialResponseVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean match = new AntPathMatcher().match("/seckill/kill", request.getRequestURI());
        if (match) {
            SocialResponseVo attribute = (SocialResponseVo) request.getSession()
                    .getAttribute(SocialLoginConstant.SOCIAL_LOGIN_CONSTANT);
            if (attribute != null) {
                // 已登录
                threadLocal.set(attribute);
                return true;
            } else {
                // 未登录
                request.getSession().setAttribute("msg", "请先登录");
                response.sendRedirect("http://auth.gulimall.com/login.html");
                return false;
            }
        }
        return true;
    }
}
